package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SearchRule;
import com.ruoyi.system.service.ISearchRuleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 检索规则Controller
 *
 * @author ruoyi
 * @date 2024-08-22
 */
@Controller
@RequestMapping("/system/searchRule")
public class SearchRuleController extends BaseController {
    private String prefix = "system/searchRule";

    @Autowired
    private ISearchRuleService searchRuleService;

    @RequiresPermissions("system:searchRule:view")
    @GetMapping()
    public String searchRule() {
        return prefix + "/searchRule";
    }

    /**
     * 查询检索规则列表
     */
    @RequiresPermissions("system:searchRule:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SearchRule searchRule) {
        startPage();
        List<SearchRule> list = searchRuleService.selectSearchRuleList(searchRule);
        return getDataTable(list);
    }

    /**
     * 导出检索规则列表
     */
    @RequiresPermissions("system:searchRule:export")
    @Log(title = "检索规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SearchRule searchRule) {
        List<SearchRule> list = searchRuleService.selectSearchRuleList(searchRule);
        ExcelUtil<SearchRule> util = new ExcelUtil<SearchRule>(SearchRule.class);
        return util.exportExcel(list, "检索规则数据");
    }

    /**
     * 新增检索规则
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存检索规则
     */
    @RequiresPermissions("system:searchRule:add")
    @Log(title = "检索规则", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SearchRule searchRule) {
        if (StringUtils.isEmpty(searchRule.getWordToSearch()) && (searchRule.getEvtTypeId2()==null || searchRule.getEvtTypeId2()<=0)) {
            return error("搜索文字为空时二级分类不能为空");
        }
        searchRule.setStatus(UserConstants.NORMAL);
        return toAjax(searchRuleService.insertSearchRule(searchRule));
    }

    /**
     * 修改检索规则
     */
    @RequiresPermissions("system:searchRule:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SearchRule searchRule = searchRuleService.selectSearchRuleById(id);
        mmap.put("searchRule", searchRule);
        return prefix + "/edit";
    }

    /**
     * 修改保存检索规则
     */
    @RequiresPermissions("system:searchRule:edit")
    @Log(title = "检索规则", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SearchRule searchRule) {
        if (StringUtils.isEmpty(searchRule.getWordToSearch()) && (searchRule.getEvtTypeId2()==null || searchRule.getEvtTypeId2()<=0)) {
            return error("搜索文字为空时二级分类不能为空");
        }
        return toAjax(searchRuleService.updateSearchRule(searchRule));
    }

    /**
     * 删除检索规则
     */
    @RequiresPermissions("system:searchRule:remove")
    @Log(title = "检索规则", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(searchRuleService.deleteSearchRuleByIds(ids));
    }

    /**
     * 状态修改
     */
    @RequiresPermissions("system:searchRule:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SearchRule searchRule)
    {
        return toAjax(searchRuleService.changeStatus(searchRule));
    }
}
